<?php
/*
 * 权限控制
 */

namespace Dms\Controller;
use Dms\Controller\CommonController;

class PermController extends CommonController
{
    public function group()
    {
    	$group = M('auth_group')->select();
    	foreach ($group as $key => $value)
    	{
    		$uidArr = M('auth_group_access')->where(array('group_id'=>$value['id']))->getField('uid',true);
    		$userArr = M('user')->where(array('id'=>array('in',$uidArr)))->getField('userName',true);
    		$group[$key]['user'] = implode(",", $userArr);
    	}
    	$this->group = $group;
    	$this->menuID = 7;
    	$this->menuName = '权限分组';
    	$this->display();
    }

    public function addGroup(){

        if(IS_POST){

        	$data = array();
        	
            $data['title']  = I("post.title");
            $data['status'] = I("post.status");

            if(empty($data['title']) == true)
            {
            	$this->error('请写入分组名称');
            }
            
            M('auth_group')->add($data);
            
            $this->success('添加成功！',U('Home/Perm/group'));
            
            exit;

        }
        $this->menuID = 7;
        $this->menuName = '添加分组';
        $this->display();
    }

    public function editGroup()
    {
    	$id = I("get.id",0);
    	$group = M('auth_group')->find($id);

    	if(empty($group)==true)
    	{
    		$this->error('错误操作');
    	}

    	if(IS_POST)
    	{
    		$title = I("post.title");
    		$status = I("post.status");

    		$data = array();

    		$data['title'] = $title;
    		$data['status'] = $status;

    		M('auth_group')->where(array('id'=>$id))->save($data);

            $this->success('修改成功！',U(MODULE_NAME.'/Perm/group'));
            
    		exit;
    	}

    	$this->group = $group;
    	$this->menuID = 7;
    	$this->menuName = '编辑分组';
    	$this->display();
    }
    
    public function editGroupAuth()
    {
    	$id = I("get.id",0);
    	$group = M('auth_group')->find($id);
    
    	if(empty($group)==true)
    	{
    		$this->error('错误操作');
    	}
    
    	if(IS_POST)
    	{
    		$action = I("post.action");
    
    		$data = array();
    
    		$data['rules'] = implode(",", $action);
    
    		M('auth_group')->where(array('id'=>$id))->save($data);
    
    		$this->success('修改成功！',U(MODULE_NAME.'/Perm/group'));
    
    		exit;
    	}
    
    	$group['rules'] = explode(',', $group['rules']);
    
    	$rule = M('auth_rule')->where(array('status'=>1))->select();
    	$controller = array();
    	foreach ($rule as $key => $value)
    	{
    		$tmp = explode("/", $value['name']);
    		if(!is_array($controller[$tmp[2]]))
    		{
    			$controller[$tmp[2]] = array();
    		}
    		if(in_array($value['id'], $group['rules']))
    		{
    			$rule[$key]['checked'] = ' checked="checked" ';
    		}
    		array_push($controller[$tmp[2]],$rule[$key]);
    	}
    	$this->controller = $controller;
    	$this->group = $group;
    	$this->menuID = 7;
    	$this->menuName = '分组权限';
    	$this->display();
    }

    public function delGroup()
    {
    	$id = I("get.id",0);
    	$group = M('auth_group')->find($id);

    	if(empty($group)==true)
    	{
    		$this->error('错误操作');
    	}

    	M('auth_group')->delete($id);

    	$this->success('删除成功',U(MODULE_NAME.'/Perm/group'));
    }

    public function node()
    {
    	$rule = M('auth_rule')->where(array('status'=>1))->select();
    	$controller = array();
    	foreach ($rule as $key => $value)
    	{
    		$tmp = explode("/", $value['name']);
    		if(!is_array($controller[$tmp[2]]))
    		{
    			$controller[$tmp[2]] = array();
    		}
    		array_push($controller[$tmp[2]],$value);
    	}
    	$this->controller = $controller;
    	$this->menuID = 6;
    	$this->menuName = '权限列表';
    	$this->display();
    }

    public function addNode()
    {
    	if(IS_POST)
    	{
    		$name = I("post.name");
    		$title = I("post.title");

    		if(empty($name) == true || empty($title) == true)
    		{
    			$this->error('请确认必填项');
    		}

    		$tmp = M('auth_rule')->where(array('name'=>$name))->find();

    		if(empty($tmp) == false)
    		{
    			$this->error('已存在节点');
    		}

    		$tmp = M('auth_rule')->where(array('title'=>$title))->find();

    		if(empty($tmp) == false)
    		{
    			$this->error('已存在节点名称');
    		}

    		$data = array(
    			'name' => $name,
    			'title' => $title,
    			'status' => 1,
    			'type' => 1,
    		);

    		M('auth_rule')->add($data);

    		$this->success('添加成功',U(MODULE_NAME.'/Perm/node'));
    		exit;
    	}

    	$this->menuID = 6;
    	$this->menuName = '添加节点';
    	
    	$this->display();
    }

    public function editNode()
    {
    	$id = I("get.id",0);
    	$node = M('auth_rule')->find($id);

    	if(empty($node)==true)
    	{
    		$this->error('错误操作');
    	}

    	if(IS_POST)
    	{
    		$name = I("post.name");
    		$title = I("post.title");

    		if(empty($name) == true || empty($title) == true)
    		{
    			$this->error('请确认必填项');
    		}

    		$tmp = M('auth_rule')->where(array('name'=>$name))->find();

    		if(empty($tmp) == false && $tmp['id'] != $node['id'])
    		{
    			$this->error('已存在节点');
    		}

    		$tmp = M('auth_rule')->where(array('title'=>$title))->find();

    		if(empty($tmp) == false && $tmp['id'] != $node['id'])
    		{
    			$this->error('已存在节点名称');
    		}

    		$data = array(
    			'name' => $name,
    			'title' => $title,
    		);

    		M('auth_rule')->where(array('id'=>$id))->save($data);

    		$this->success('修改成功',U(MODULE_NAME.'/Perm/node'));
    		exit;
    	}

    	$this->node = $node;
    	$this->menuID = 6;
    	$this->menuName = '编辑节点';
    	$this->display();
    }

    public function delNode()
    {
    	$id = I("get.id",0);
    	$node = M('auth_rule')->find($id);

    	if(empty($node)==true)
    	{
    		$this->error('错误操作');
    	}

    	M('auth_rule')->delete($id);

    	$this->success('删除成功',U(MODULE_NAME.'/Perm/node'));
    }
}